import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class ProbabilityQueue {
    Map<Double, String> map = new LinkedHashMap<Double, String>();

    public ProbabilityQueue(Map<Double, String> teamsAndProbs) throws Exception {
        map = new LinkedHashMap<Double, String>();

        double sum = 0;
        map.putAll(teamsAndProbs);
        for(Double d : map.keySet())
        {
            sum+=d;
        }
        System.out.println("Sum is "+sum);
        if(sum != 1) {
            throw new Exception("The probabilities entered do not sum to 1!");
        }
    }

    public String pullTeamFromQueue() {
        Random r = new Random();
        String value = null;

        while(value == null) {
            double chance = r.nextDouble();
            double probLeft = 1;
            for(double probability : map.keySet()) {
                probLeft-=probability;
                if(chance > probLeft && chance <= probLeft+probability) {
                    value = map.get(probability);
                    map.remove(probability);
                    break;
                }
            }
        }
        return value;
    }

    public boolean isEmpty() {
        return map.isEmpty();
    }
}

